<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拦截器</title>
<!--     <script src="../node_modules/axios/dist/mine-axios.js"></script>-->
</head>
<body>
    <script type="text/javascript">
        //构造函数
        function Axios(config) {
            this.config = config;
            this.interceptors = {
                request: new InterceptorManager(),
                response: new InterceptorManager(),
            }
        }

        Axios.prototype.request = function (config) {
            //创建一个promise对象
            let p = Promise.resolve(config);
            //创建一个数组
            const chain = [dispatchRequest, undefined];
            //处理拦截器
            //请求拦截器，将请求拦截器的回调压入chain的前面
            this.interceptors.request.handlers.forEach((item) => {
                chain.unshift(item.resolved, item.rejected);
            });
            //响应拦截器：从后面添加
            this.interceptors.response.handlers.forEach((item) => {
                chain.push(item.resolved, item.rejected);
            });
            while (chain.length > 0) {
                p = p.then(chain.shift(), chain.shift());
            }
            return p;
        }

        function dispatchRequest(config) {
            return new Promise((resolve, reject) => {
                resolve({
                    status: 200,
                    statusText: 'OK'
                });
            });
        }

        //创建实例
        let context = new Axios({});
        //创建axios函数
        let axios = Axios.prototype.request.bind(context);
        //将context属性config、interceptors添加至axios函数对象身上
        Object.keys(context).forEach((key) => {
            axios[key] = context[key];
        });
        console.dir(axios);

        //拦截器管理器的构造函数
        function InterceptorManager() {
            this.handlers = [];
        }

        InterceptorManager.prototype.use = function (resolved, rejected) {
            this.handlers.push({resolved, rejected});
        }


        <!--region 测试代码-->
        // 设置请求拦截器
        axios.interceptors.request.use(function one(config) {
            console.log("request interceptor success-1");
            // 修改config中的参数
            config.params = {a: 100};
            return config;
        }, function one(error) {
            console.log("request interceptor failure-1");
            return Promise.reject(error);
        });
        axios.interceptors.request.use(function two(config) {
            console.log("request interceptor success-2");
            // 修改config中的参数
            config.timeout = 2000;
            return config;
        }, function two(error) {
            console.log("request interceptor failure-2");
            return Promise.reject(error);
        });

        // 设置响应拦截器
        axios.interceptors.response.use(function (response) {
            console.log("response interceptor success-1");
            // console.log(response);
            // console.log(response.data);
            // return response;
            return response.data;
        }, function (error) {
            console.log("response interceptor failure-1");
            return Promise.reject(error);
        });

        axios.interceptors.response.use(function (response) {
            console.log("response interceptor success-2");
            return response;
        }, function (error) {
            console.log("response interceptor failure-2");
            return Promise.reject(error);
        });

        console.dir(axios);

        // 发送请求
        axios({
            method: 'GET',
            url: 'http://localhost:3000/posts'
        }).then(resp => {
            console.log('自定义回调，处理成功的结果');
            console.log(resp);
        }).catch((reason)=>{
            console.log('自定义失败回调');
        });
        <!--endregion-->
    </script>
</body>
</html>